package com.cooper.mediadb.online;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cooper.scraper.DownloadException;
import com.cooper.scraper.HttpDownload;
import com.cooper.scraper.ScraperException;

public class AnalyzeYUGAOPIAN extends AnalyzeVideo {

	private static final String MTIME_REG = "flashData\\.swf = 'http://.+?\\.mtime\\..+?/static/flash/outplayer.swf\\?vid=(.*?)&mid=(.*?)(&.*?)?';";
	private static final String MTIME_FILE_REG = "flashData\\.flashvars\\s*=\\s*'vcastr_file=(http://.+?\\.mtime\\.com/[^&]+)&";
	private static final String YOUKU_REG = "flashData\\.swf = 'http://.+?\\.youku\\.com/player\\.php/partnerid/XODQw/sid/(.+?)/v.swf';";
	// http://v.youku.com/v_show/id_XNDE2NTU0MTk2.html
	private static final String QQ_REG = "flashData\\.swf = 'http://.+?\\.video\\.qq\\.com/TPout\\.swf.+?vid=((\\w).+?(\\w)(\\w))';";
	// http://v.qq.com/boke/page/u/l/7/u0401u384l7.html
	private static final String JWPLAYER_REG = "flashData\\.swf = 'images/player.swf\\?file=(.+?)';";
	private static final String QUICKTIME_PLAYER = "<object[^>]*>(<param[^>]*>)*?<embed src=\"([^\"]*)\".*?pluginspage=\"http://www.apple.com/quicktime/";
	private static final String TRAILER_ADDICT_REG = "flashData\\.swf = 'http://www\\.traileraddict\\.com/[^/]+/(\\w+?)';";
	private static final String MOVIEWEB_REG = "flashData\\.swf = '(http://www\\.movieweb\\.com.+?)';";
	private static final String V56_REG = "flashData\\.swf = '(http://player\\.56\\.com/.+?)';";
	private static final String QIYI_REG="flashData\\.swf = 'http://player\\.video\\.qiyi\\.com/([^\\.]*)\\.swf';";
	
	public AnalyzeYUGAOPIAN(HttpDownload httpDownload, String url, String format) {
		super(httpDownload, url, format);
	}

	@Override
	public List<String> analyzeVideo() throws DownloadException,
			ScraperException, IOException {
		String page = httpDownload.downloadPage(url);

		Pattern p = Pattern.compile(YOUKU_REG);
		Matcher m = p.matcher(page);
		if (m.find()) {
			return AnalyzeVideo.createAnalyzeVideo(
					httpDownload,
					new MediaSource("http://v.youku.com/v_show/id_"
							+ m.group(1).trim() + ".html",
							MediaSrcMgr.SRC_YOUKU), format).analyzeVideo();
		}
		p = Pattern.compile(V56_REG);
		m = p.matcher(page);
		if (m.find()) {
			return AnalyzeVideo.createAnalyzeVideo(httpDownload,
					new MediaSource(m.group(1), MediaSrcMgr.SRC_56), format)
					.analyzeVideo();
		}

		p = Pattern.compile(QQ_REG);
		m = p.matcher(page);
		if (m.find()) {
			return AnalyzeVideo.createAnalyzeVideo(
					httpDownload,
					new MediaSource("http://v.qq.com/boke/page/" + m.group(2)
							+ "/" + m.group(3) + "/" + m.group(4) + "/"
							+ m.group(1).trim() + ".html", MediaSrcMgr.SRC_QQ),
					format).analyzeVideo();
		}
		p = Pattern.compile(MTIME_REG);
		m = p.matcher(page);
		if (m.find()) {
			return AnalyzeVideo.createAnalyzeVideo(
					httpDownload,
					new MediaSource("http://movie.mtime.com/" + m.group(2)
							+ "/trailer/" + m.group(1) + ".html",
							MediaSrcMgr.SRC_MTIME), format).analyzeVideo();
		}

		m = Pattern.compile(QIYI_REG).matcher(page);
		if (m.find()){
			AnalyzeQIYI analyzeQIYI = new AnalyzeQIYI(httpDownload,format);
			analyzeQIYI.setCurrentVedioID(m.group(1));
			return analyzeQIYI.analyzeVideo();
		}
		
		m = Pattern.compile(TRAILER_ADDICT_REG).matcher(page);
		if (m.find()) {
			return new AnalyzeTraileraddict(m.group(1), httpDownload, format)
					.analyzeVideo();
		}

		m = Pattern.compile(MOVIEWEB_REG).matcher(page);
		if (m.find()) {
			return new AnalyzeMovieweb(httpDownload, m.group(1), format)
					.analyzeVideo();
		}

		List<String> result = new ArrayList<String>();

		m = Pattern.compile(MTIME_FILE_REG).matcher(page);
		if (m.find()) {
			result.add(m.group(1));
			return result;
		}

		p = Pattern.compile(JWPLAYER_REG);
		m = p.matcher(page);
		if (m.find()) {
			result.add(m.group(1));
		} else {
			p = Pattern.compile(QUICKTIME_PLAYER);
			m = p.matcher(page);
			if (m.find()) {
				result.add(m.group(2));
			}
		}

		return result;
	}
}
